package com.tma.rest;

import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.ws.rs.*;

import com.tma.implementation.GraphManagementService;
import com.tma.model.GraphObject;
import com.tma.model.JsonMapper;
import com.tma.utils.JsonUtils;
import org.jboss.resteasy.annotations.providers.jaxb.json.BadgerFish;

@Path("/graph")
public class GraphRestService {
	private static final Logger LOGGER = Logger.getLogger(GraphRestService.class.getName());
		
	/**
	 * @method: API published to client to create a graph
	 * @param graphname
	 * @param graphdescription
	 * @return jsonString declares the operation failed or success
	 */
	
	@BadgerFish
	@POST
	@Produces("application/json")
	@Path("/post/graphname={graphname}&graphdescription={graphdescription}")
	public JsonMapper createGraph(@PathParam("graphname") String graphname, @PathParam("graphdescription") String graphdescription) {
				
		LOGGER.log(Level.INFO, "Graph create : " + "graphname = " + graphname + ", graphdescription = " + graphdescription);
		
		GraphManagementService graphService = GraphManagementService.getService();		
		boolean result = graphService.createGraph(graphname, graphdescription);
		String response = (result == true ? "success" : "failed");
		response = JsonUtils.buildStringReponseValue("create", response);
		
		JsonMapper mapper = new JsonMapper(response);
		return mapper;
	}
	
	/**
	 * @method: API published to client to delete a graph
	 * @param graphname
	 * @return jsonString declares the operation failed or success
	 */
	@BadgerFish
	@Path("/delete/graphname={graphname}")
	@DELETE
	@Produces("application/json")
	public JsonMapper deleteGraph(@PathParam("graphname") String graphname) {

		LOGGER.log(Level.INFO, "graph delete : " + "graphname = " + graphname);
		
		GraphManagementService graphService = GraphManagementService.getService();		
		boolean result = graphService.deleteGraph(graphname);
		String response = (result == true ? "success" : "failed");	
		response = JsonUtils.buildStringReponseValue("delete", response);
		
		JsonMapper mapper = new JsonMapper(response);
		return mapper;
	}
	
	/**
	 * @method: API published to client to update a graph description
	 * @param graphname
	 * @param graphdescription
	 * @return jsonString declares the operation failed or success
	 */
	@BadgerFish
	@Path("/update/graphname={graphname}&graphdescription={graphdescription}")
	@PUT
	@Produces("application/json")
	public JsonMapper updateGraph(@PathParam("graphname") String graphname, @PathParam("graphdescription") String graphdescription) {

		LOGGER.log(Level.INFO, "Graph update : " + "graphname = " + graphname + ", graphdescription = " + graphdescription);
		
		GraphManagementService graphService = GraphManagementService.getService();		
		boolean result = graphService.updateGraph(graphname, graphdescription);
		String response = (result == true ? "success" : "failed");
		response = JsonUtils.buildStringReponseValue("update", response);
		
		JsonMapper mapper = new JsonMapper(response);
		return mapper;
	}
	
	/**
	 * @method: API published to client to retrieve a graph description
	 * @param graphname: name of the graph
	 * @return jsonString contains graph description
	 */
	@BadgerFish
	@Path("/get/graphname={graphname}")
	@GET
	@Produces("application/json")
	public JsonMapper retriveGraph(@PathParam("graphname") String graphname) {

		LOGGER.log(Level.INFO, "graph get : " + "graphname = " + graphname);
		JsonMapper mapper = new JsonMapper();
		
		GraphObject graph = GraphManagementService.getService().getGraph(graphname);
		if(graph == null)
		{
			String response = "failed";	
			response = JsonUtils.buildStringReponseValue("get", response);
			mapper.setJson(response);
		}
		else
		{
			String response = JsonUtils.buildStringReponseGetAGraph(graph);	
			mapper.setJson(response);
		}
		
		return mapper;
	}
	
	/**
	 * @method: API published to client to retrieve a list of graphs
	 * @return jsonString contains list of graph descriptions
	 */
	@BadgerFish
	@Path("/getallgraph")
	@GET
	@Produces("application/json")
	public JsonMapper retriveAllGraph() {
		
		LOGGER.log(Level.INFO, "get all graph : ");
		
		ArrayList<GraphObject> graphs = GraphManagementService.getService().getAllGraph();
		String response = JsonUtils.buildStringReponseGetAllGraph(graphs);
		
		JsonMapper mapper = new JsonMapper(response);
		return mapper;		
	}
	
}
